package com.zhuangfei.hos_timetableview.ability;

import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnSelectListener;
import com.zhuangfei.hos_timetableview.ResourceTable;
import com.zhuangfei.hos_timetableview.model.MySubject;
import com.zhuangfei.hos_timetableview.model.SubjectRepertory;
import com.zhuangfei.timetable.TimetableView;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.StackLayout;
import ohos.agp.utils.Color;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 颜色池示例，展示颜色池的相关用法
 */
public class ColorPoolAbility extends Ability {

    TimetableView mTimetableView;
    Button moreButton;
    String[] popupMenus;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_color_pool);
        // 添加表头的include
        StackLayout stInclude = (StackLayout) findComponentById(ResourceTable.Id_st_include);
        stInclude.addComponent(LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_header_simple, stInclude, false));
        moreButton = (Button) findComponentById(ResourceTable.Id_id_more);
        moreButton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                showPopmenu();
            }
        });
        initTimetableView();
    }

    /**
     * 初始化课程控件
     */
    private void initTimetableView() {
        mTimetableView = (TimetableView) findComponentById(ResourceTable.Id_id_timetableView);
        List<MySubject> mySubjects = SubjectRepertory.loadDefaultSubjects();
        mTimetableView.source(mySubjects)
                .curWeek(1)
                .curTerm("大三下学期")
                .showView();
    }

    /**
     * 显示弹出菜单
     */
    public void showPopmenu() {
        if (popupMenus == null) {
            popupMenus = new String[]{"指定三种颜色", "默认颜色池", "扩充颜色池", "指定课程颜色"};
        }
        new XPopup.Builder(getContext())
                .hasShadowBg(false)
                .atView(moreButton)
                .asAttachList(popupMenus, null,
                        new OnSelectListener() {
                            @Override
                            public void onSelect(int position, String text) {
                                switch (text) {
                                    case "指定三种颜色":
                                        setColor(Color.BLUE.getValue(), Color.YELLOW.getValue(), Color.CYAN.getValue());
                                        break;
                                    case "默认颜色池":
                                        resetColor();
                                        break;
                                    case "扩充颜色池":
                                        addColor(Color.BLUE.getValue(), Color.YELLOW.getValue(), Color.CYAN.getValue());
                                        break;
                                    case "指定课程颜色":
                                        forColor();
                                        break;
                                    default:
                                        break;
                                }
                            }
                        }, 0, 0).show();
    }

    /**
     * 设置指定的颜色，默认情况下颜色池中有一些颜色
     * 所以这里需要先清空一下颜色池
     *
     * @param colors 颜色数组
     */
    public void setColor(int... colors) {
        mTimetableView.colorPool().clear().add(colors);
        mTimetableView.updateView();
    }

    /**
     * 重置颜色池
     */
    public void resetColor() {
        mTimetableView.colorPool().reset();
        mTimetableView.updateView();
    }

    /**
     * 追加颜色
     *
     * @param colors 颜色数组
     */
    public void addColor(int... colors) {
        mTimetableView.colorPool().add(colors);
        mTimetableView.updateView();
    }

    /**
     * 指定课程的颜色，未指定的课程自动分配
     */
    public void forColor() {
        Map<String, Integer> colorMap = new HashMap<>();
        colorMap.put("数字图像处理", Color.RED.getValue());
        colorMap.put("算法分析与设计", Color.BLUE.getValue());
        mTimetableView.colorPool().setIgnoreUserlessColor(false).setColorMap(colorMap);
        mTimetableView.updateView();
    }
}
